//
//  ICBaseNoticeCell.m
//  ICome
//
//  Created by zhangrongwu on 2017/7/21.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICBaseNoticeCell.h"
#import "ICMessageNetworkManager.h"
#import "ICHeaderFile.h"

@interface ICBaseNoticeCell()



@end

@implementation ICBaseNoticeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.unreadPoint];
        
        [self.bgView addSubview:self.headImageView];
        [self.bgView addSubview:self.titleL];
        
        [self.bgView addSubview:self.lineView];
        [self.bgView addSubview:self.contentV];
        
        [self setConstraint];
        
    }
    return self;
}

-(void)setModelFrame:(ICMessageFrame *)modelFrame {
    _modelFrame = modelFrame;
    
    NSDictionary *filekey =  [NSDictionary dictionaryWithJsonString:modelFrame.model.message.fileKey];
    NSString *type = modelFrame.model.message.type;
    NSString *from = modelFrame.model.message.from;
    if ([type isEqualToString:TypeDrtNews]) {
        from = filekey[@"dId"];
    }
    WEAKSELF
    [ICMessageDatabase getBaseInfoWithgId:from finish:^(NSArray *array, NSError *error) {
        if (array.count > 0) {
            ICUser *info = [array firstObject];
            weakSelf.titleL.text = info.eName;
            [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(info.photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
        } else {
            [[ICMessageNetworkManager sharedInstance] getFromInfoWithParam:@{@"id":from} InfoCache:^(id responseCache) {
                NSString *name = [[responseCache objectForKey:@"data"] objectForKey:@"name"];
                NSString *photoId = [[responseCache objectForKey:@"data"] objectForKey:@"photoId"];
                weakSelf.titleL.text =  name;
                [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
            } success:^(id object) {
                NSString *name = [[object objectForKey:@"data"] objectForKey:@"name"];
                NSString *photoId = [[object objectForKey:@"data"] objectForKey:@"photoId"];
                weakSelf.titleL.text =  name;
                [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
            } failure:nil];
        }
    }];
    
    if (modelFrame.model.message.status == ICMessageStatus_unRead) {
        self.unreadPoint.hidden = NO;
    } else {
        self.unreadPoint.hidden = YES;
    }
}

#pragma mark - Constraint
- (void)setConstraint
{
    [self.unreadPoint mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.centerY.equalTo(self.headImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.unreadPoint.mas_right).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-38);
        make.top.equalTo(self.contentView.mas_top).offset(5);
        make.bottom.equalTo(self.mas_bottom).offset(-5);
    }];
    
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.top.equalTo(self.bgView.mas_top).offset(12);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(8);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.centerY.equalTo(self.headImageView.mas_centerY);
        make.height.mas_equalTo(23);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.right.equalTo(self.bgView.mas_right).offset(-12);
        make.top.equalTo(self.bgView.mas_top).offset(50);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.contentV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(15);
//        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.top.equalTo(self.lineView.mas_bottom).offset(10);
        make.width.mas_equalTo(App_Frame_Width-60);
        make.height.mas_equalTo(11);
    }];
}

- (UIImageView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIImageView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius  = 5;
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.borderColor = ICRGB(0xd9d9d9).CGColor;
        _bgView.layer.borderWidth = 0.5;
    }
    return _bgView;
}


-(UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}

- (UILabel *)titleL
{
    if (!_titleL) {
        _titleL        = [[UILabel alloc] init];
        _titleL.textColor = ICTEXTCOLOR_T1;
        _titleL.font = [UIFont systemFontOfSize:16];
        
    }
    return _titleL;
}

-(UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = ICRGB(0xd9d9d9);
    }
    return _lineView;
}

-(UIView *)contentV
{
    if(!_contentV)
    {
        _contentV = [[UIView alloc] init];
        _contentV.backgroundColor = [UIColor clearColor];
    }
    return _contentV;
}

-(UILabel *)unreadPoint {
    if (!_unreadPoint) {
        _unreadPoint = [[UILabel alloc] init];
        _unreadPoint.backgroundColor = [UIColor redColor];
        _unreadPoint.layer.masksToBounds = YES;
        _unreadPoint.layer.cornerRadius = 6;
        [self.contentView addSubview:_unreadPoint];
    }
    return _unreadPoint;
}


@end
